package com.sun.weekend.util;

import com.sun.weekend.beans.UserBean;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class UserProxy {

	public UserProxy(){
	}

	public void signUp(String userName,String password,String email){
		UserBean user = new UserBean();
		user.setUsername(userName);
		user.setPassword(password);
		user.setEmail(email);
		user.setSex(true);
		user.setSignature("这个家伙很懒，什么也不说。。。");
		user.signUp(new SaveListener<UserBean>() {
			@Override
			public void done(UserBean userBean, BmobException e) {
				if(userBean != null && signUpLister != null){
					signUpLister.onSignUpSuccess();
				}else{
					signUpLister.onSignUpFailure(e.getMessage());
				}
			}
		});
	}

	public UserBean getCurrentUser(){
		UserBean user = BmobUser.getCurrentUser(UserBean.class);
		if(user != null){
			LogUtils.logi("本地用户信息" + user.getObjectId() + "-"
					+ user.getUsername() + "-"
					+ user.getSessionToken() + "-"
					+ user.getCreatedAt() + "-"
					+ user.getUpdatedAt() + "-"
					+ user.getSignature() + "-"
					+ user.getSex());
			return user;
		}else{
			LogUtils.logi("本地用户为null,请登录。");
		}
		return null;
	}
	
	public void login(String userName,String password){

		UserBean user = new UserBean();
		user.setUsername(userName);
		user.setPassword(password);
		user.login(new SaveListener<UserBean>() {
			@Override
			public void done(UserBean userBean, BmobException e) {
				if (e == null) {

					LogUtils.logi(e.getMessage());
				}
				if (userBean != null && loginListener != null) {
					loginListener.onLoginSuccess();
				}else{
					loginListener.onLoginFailure(e.getMessage());
				}
			}
		});
	}

	public void logout(){
		BmobUser.logOut();
	}
	
	public void update(String... args){
		UserBean user = getCurrentUser();
		user.setUsername(args[0]);
		user.setEmail(args[1]);
		user.setPassword(args[2]);
		user.setSex(Boolean.parseBoolean(args[3]));
		user.setSignature(args[4]);
		//...
		user.update(new UpdateListener() {
			@Override
			public void done(BmobException e) {
				if(updateListener != null){
					updateListener.onUpdateSuccess();
				}
			}
		});
	}

	public void resetPassword(String email){
		BmobUser.resetPasswordByEmail(email, new UpdateListener() {
			@Override
			public void done(BmobException e) {
				if(resetPasswordListener != null){
					resetPasswordListener.onResetSuccess();
				}
			}
		});
	}
	private IResetPasswordListener resetPasswordListener;
	private ISignUpListener signUpLister;
	private ILoginListener loginListener;
	private IUpdateListener updateListener;
	public void setOnSignUpListener(ISignUpListener signUpLister){
		this.signUpLister = signUpLister;
	}
	public void setOnUpdateListener(IUpdateListener updateListener){
		this.updateListener = updateListener;
	}
	public void setOnLoginListener(ILoginListener loginListener){
		this.loginListener  = loginListener;
	}

	public void setOnResetPasswordListener(IResetPasswordListener resetPasswordListener){
		this.resetPasswordListener = resetPasswordListener;
	}

	public interface ISignUpListener{
		void onSignUpSuccess();
		void onSignUpFailure(String msg);
	}

	public interface ILoginListener{
		void onLoginSuccess();
		void onLoginFailure(String msg);
	}

	public interface IResetPasswordListener{
		void onResetSuccess();
		void onResetFailure(String msg);
	}
	public interface IUpdateListener{
		void onUpdateSuccess();
		void onUpdateFailure(String msg);
	}

}
